﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChmLauncher
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var arg in args)
            {
                sb.Append(" ");
                sb.Append(arg);
            }
            var argsText = sb.ToString().Trim();

            if (System.IO.Directory.Exists(argsText) == false)
            {
                Environment.Exit(0);
                return;
            }

            if (argsText.EndsWith("\\") == false)
                argsText += "\\";

            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(argsText);

            var chmFilePath = argsText + di.Name + ".chm";

            if (System.IO.File.Exists(chmFilePath) == false)
            {
                chmFilePath = argsText + "$_Chms~\\" + di.Name + ".chm";
                if (System.IO.File.Exists(chmFilePath) == false)
                {
                    Environment.Exit(0);
                    return;
                }
            }

            if (chmFilePath.Contains(" "))
                chmFilePath = "\"" + chmFilePath + "\"";

            System.Diagnostics.Process.Start("explorer.exe", chmFilePath);
            Environment.Exit(0);
        }
    }
}
